package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.SingleLineViewNew;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能: 考勤申请汇总查询 Fragment
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/2/14 18:00
 */
@SuppressWarnings("deprecation") public class AttendanceSummaryListQueryFragment extends Fragment
    implements View.OnClickListener {

  protected static final String EXTRA_STAFF =
      "com.isunland.managesystem.ui.AttendanceSummaryListQueryFragment.EXTRA_STAFF";
  protected static final String EXTRA_DATASTATUS =
      "com.isunland.managesystem.ui.AttendanceSummaryListQueryFragment.EXTRA_DATASTATUS";
  protected static final String EXTRA_DATATIME =
      "com.isunland.managesystem.ui.AttendanceSummaryListQueryFragment.EXTRA_DATATIME";
  protected static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.AttendanceSummaryListQueryFragment.EXTRA_TYPE";

  protected static final String VALUE_OVER = "overTime";
  protected static final String VALUE_UNPUNCH = "unpunch";
  protected static final String VALUE_HOLY = "holy";
  protected static final String VALUE_TRIP = "trip";
  protected static final String VALUE_OUT = "outWork";
  protected static final String VALUE_OFF = "useablePayHoly";

  private final int REQUEST_STAFF = 0;
  private final int REQUEST_DATASTATUS = 1;
  private final int REQUEST_STARTTIME = 2;
  private final int REQUEST_ENDTIME = 3;

  private SingleLineViewNew tvStaffName;
  private SingleLineViewNew tvDataStatus;
  private SingleLineViewNew tvStartTime;
  private SingleLineViewNew tvEndTime;
  private TextView cbHoly;
  private TextView cbOverTime;
  private TextView cbOutWork;
  private TextView cbTrip;
  private TextView cbUnpunch;
  private TextView cbUseablePayHoly;

  private CustomerDialog regStaff;//申请人(name,code)
  private CustomerDialog dataStatus;//数据状态
  private CustomerDialog dataTime;//时间
  private HashMap<String, String> typeMap = new HashMap<>();//考勤申请单类型

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity mactivity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar = mactivity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setTitle(R.string.query);
    }
    regStaff = (CustomerDialog) getArguments().getSerializable(EXTRA_STAFF);
    dataStatus = (CustomerDialog) getArguments().getSerializable(EXTRA_DATASTATUS);
    dataTime = (CustomerDialog) getArguments().getSerializable(EXTRA_DATATIME);
    //noinspection unchecked
    typeMap = (HashMap<String, String>) getArguments().getSerializable(EXTRA_TYPE);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_confirm:

        if (!typeMap.containsValue("T")) {
          ToastUtil.show(R.string.atLeastChooseOne);
        } else {
          Intent value = new Intent();
          value.putExtra(EXTRA_STAFF, regStaff);
          value.putExtra(EXTRA_DATASTATUS, dataStatus);
          value.putExtra(EXTRA_DATATIME, dataTime);
          value.putExtra(EXTRA_TYPE, typeMap);
          getActivity().setResult(Activity.RESULT_OK, value);
          getActivity().finish();
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_attendance_summary_query, container, false);
    findViews(view);
    initData();
    return view;
  }

  private void findViews(View view) {
    tvStaffName = (SingleLineViewNew) view.findViewById(R.id.tv_staffName);
    tvDataStatus = (SingleLineViewNew) view.findViewById(R.id.tv_titleRight);
    tvStartTime = (SingleLineViewNew) view.findViewById(R.id.tv_startTime);
    tvEndTime = (SingleLineViewNew) view.findViewById(R.id.tv_endTime);

    cbHoly = (TextView) view.findViewById(R.id.cb_holy);
    cbOverTime = (TextView) view.findViewById(R.id.cb_overTime);
    cbOutWork = (TextView) view.findViewById(R.id.cb_outWork);
    cbTrip = (TextView) view.findViewById(R.id.cb_trip);
    cbUnpunch = (TextView) view.findViewById(R.id.cb_unpunch);
    cbUseablePayHoly = (TextView) view.findViewById(R.id.cb_useablePayHoly);

    tvStaffName.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        //任务汇总：人员列表
        Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, regStaff.getId());
        startActivityForResult(i, REQUEST_STAFF);
      }
    });
    tvDataStatus.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(REQUEST_DATASTATUS, MyDateUtil.stringToDate("", MyDateUtil.TYPE_DATE));
      }
    });
    tvStartTime.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(REQUEST_STARTTIME,
            MyDateUtil.stringToDate(dataTime.getId(), MyDateUtil.TYPE_DATE));
      }
    });
    tvEndTime.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        showDialog(REQUEST_ENDTIME,
            MyDateUtil.stringToDate(dataTime.getName(), MyDateUtil.TYPE_DATE));
      }
    });

    cbOutWork.setOnClickListener(this);
    cbUnpunch.setOnClickListener(this);
    cbHoly.setOnClickListener(this);
    cbTrip.setOnClickListener(this);
    cbOverTime.setOnClickListener(this);
    cbUseablePayHoly.setOnClickListener(this);
  }

  private void initData() {
    tvStaffName.setTextContent(regStaff.getName());
    tvDataStatus.setTextContent(dataStatus.getName());
    tvStartTime.setTextContent(dataTime.getId());
    tvEndTime.setTextContent(dataTime.getName());
    getCheck(cbHoly, typeMap.get(VALUE_HOLY));
    getCheck(cbOutWork, typeMap.get(VALUE_OUT));
    getCheck(cbOverTime, typeMap.get(VALUE_OVER));
    getCheck(cbTrip, typeMap.get(VALUE_TRIP));
    getCheck(cbUnpunch, typeMap.get(VALUE_UNPUNCH));
    getCheck(cbUseablePayHoly, typeMap.get(VALUE_OFF));
  }

  private void getCheck(TextView cb, String type) {
    if ("T".equalsIgnoreCase(type)) {
      cb.setTextColor(getResources().getColor(R.color.primary));
      cb.setBackgroundResource(R.drawable.textview_attendance_type_checked);
    } else {
      cb.setTextColor(getResources().getColor(R.color.standard_important));
      cb.setBackgroundResource(R.drawable.textview_attendance_type_unchecked);
    }

  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_DATASTATUS:
        dialog = new AttendanceSummaryDataStatusDialogFragment();
        break;
      case REQUEST_STARTTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;
      case REQUEST_ENDTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog != null) {
      dialog.setTargetFragment(this, requestCode);
      dialog.show(fm, "");
    }
  }



  private void getTypeMap(String typeCode, boolean ischeck) {
    if (ischeck) {
      typeMap.put(typeCode, "T");
    } else {
      typeMap.put(typeCode, "");
    }
  }

  @SuppressWarnings("deprecation") @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_STAFF) {
      String mStaffName = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      String mjobNos = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);
      regStaff.setId(mjobNos);
      regStaff.setName(mStaffName);
      tvStaffName.setTextContent(mStaffName);
    }
    if (requestCode == REQUEST_DATASTATUS) {
      dataStatus = (CustomerDialog) data.getSerializableExtra(
          AttendanceSummaryDataStatusDialogFragment.EXTRA_DATA_STATUS);
      tvDataStatus.setTextContent(dataStatus.getName());
    }
    if (requestCode == REQUEST_STARTTIME) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String mStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      dataTime.setId(mStartTime);
      tvStartTime.setTextContent(mStartTime);
    }
    if (requestCode == REQUEST_ENDTIME) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String mEndTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      dataTime.setName(mEndTime);
      tvEndTime.setTextContent(mEndTime);
    }
  }

  public static AttendanceSummaryListQueryFragment newInstance(CustomerDialog regStaff,
      CustomerDialog dataStatus, CustomerDialog dataTime, HashMap<String, String> typeMap) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_STAFF, regStaff);
    args.putSerializable(EXTRA_DATASTATUS, dataStatus);
    args.putSerializable(EXTRA_DATATIME, dataTime);
    args.putSerializable(EXTRA_TYPE, typeMap);
    AttendanceSummaryListQueryFragment fragment = new AttendanceSummaryListQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onClick(View v) {
    String typeCode = "";
    switch (v.getId()) {

      case R.id.cb_holy:
        typeCode = VALUE_HOLY;
        break;
      case R.id.cb_overTime:
        typeCode = VALUE_OVER;
        break;
      case R.id.cb_outWork:
        typeCode = VALUE_OUT;
        break;
      case R.id.cb_trip:
        typeCode = VALUE_TRIP;
        break;
      case R.id.cb_unpunch:
        typeCode = VALUE_UNPUNCH;
        break;
      case R.id.cb_useablePayHoly:
        typeCode = VALUE_OFF;
        break;
    }
    getTypeMap(typeCode, !("T".equalsIgnoreCase(typeMap.get(typeCode))));
    getCheck((TextView) v, typeMap.get(typeCode));
  }
}
